clc,clear;
figure(1);
f = figure(1);
ax = axes;
load depth;

%img=imread('topRoi.jpg');%原图
%img=imread('img1roi.jpg');%匹配点
img=imread('单峰与匹配点_方差0.12+局部.jpg');%匹配点和单峰点 即初始种子点
%depth=depth(20:255,20:344);%边缘误差较大

%-------------滤波----------%
% se = strel('disk',5);
% ao= imopen(depth,se);
% %ao=imgaussfilt(ao,5);
% ao= imclose(ao,se);

%-------------绘图-----------%
s=surf(depth);
s.CData=img;
s.FaceColor = 'texturemap'; 
s.EdgeColor = 'none';
ax.XLim = [-20 345];
ax.YLim = [-20 245];
ax.ZLim = [-5 25];
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
zlabel(ax,'深度');
set(gcf,'unit','centimeters','position',[5 5 12 5]);
set(gca,'Position',[0 0 1 1]);
view(ax,[726.2590625 44]);
grid(ax,'off');
axis(ax,'off');
hold(ax,'off');

% %------------保存gif--------------%
% for i = 1:180
%     camorbit(2,0,'data',[0 0 1]) %%[0 0 1]表示按z轴旋转。36*10=360表示旋转一周
%     drawnow %%即时显示旋转的结果
%     M=getframe(gcf);
%     nn=frame2im(M);
%     [nn,cm]=rgb2ind(nn,256);
%     if i==1
%         imwrite(nn,cm,'out3.gif','gif','LoopCount',inf,'DelayTime',0.1);%说明loopcount只是在i==1的时候才有用
%     else
%         imwrite(nn,cm,'out3.gif','gif','WriteMode','append','DelayTime',0.1)%当i>=2的时候loopcount不起作用
%     end
% end

figure(2);
clear;close all;
f = figure(2);
ax = axes;
t = hgtransform;
%img=imread('topRoi.jpg');
img=imread('单峰与匹配点_方差0.12+局部.jpg');
data=importdata('XYZ.txt');

X=data(:,1);
Y=data(:,2);
Z=data(:,3);

ylabel='y';
xlabel='x';
sca=scatter3(X,Y,Z,'.','Parent',t );
view(ax,[726.2590625 55]);
set(gcf,'unit','centimeters','position',[5 5 16 7]);

img_t=pagetranspose(img);
imgdata=reshape(img_t,[numel(img)/3 , 3]);
sca.CData=imgdata;

rx_angle = -8*pi/180; 
Rx = makehgtform('xrotate',rx_angle);
ty=-20;
Tx = makehgtform('translate',[0 ty 0]);
t.Matrix = Rx*Tx;
%------------保存gif--------------%
for i = 1:180
    camorbit(2,0,'data',[0 0 1]) %%[0 0 1]表示按z轴旋转。36*10=360表示旋转一周
    drawnow %%即时显示旋转的结果
    M=getframe(gcf);
    nn=frame2im(M);
    [nn,cm]=rgb2ind(nn,256);
    if i==1
        imwrite(nn,cm,'out4.gif','gif','LoopCount',inf,'DelayTime',0.1);%说明loopcount只是在i==1的时候才有用
    else
        imwrite(nn,cm,'out4.gif','gif','WriteMode','append','DelayTime',0.1)%当i>=2的时候loopcount不起作用
    end
end
